# -*- coding: utf-8 -*-
import os

from Config import Params
from Service.U2Syner import U2Syner
import uiautomator2 as u2


def refreshBatchCode(srcFilePath, deviceList=None, args=None):
    for i in range(len(deviceList)):
        serial = deviceList[i].serial
        print("yzprt serial:", serial)
        srcFilePath = os.path.join(Params.ROOT_DIR,srcFilePath)
        print("yzprt srcFilePath:", srcFilePath)
        dir,fileName = os.path.split(srcFilePath)
        print("yzprt dir,fileName:", dir,fileName)
        fileNa,ext = os.path.splitext(fileName)
        print("yzprt fileNa,ext:", fileNa,ext)
        with open(srcFilePath, 'r', encoding='utf-8', errors='ignore') as f:
            ss = f.read()
        print("yzprt Params.ROOT_DIR:", Params.ROOT_DIR)
        distFilePath = os.path.join(Params.ROOT_DIR, f"Batches/{fileNa}_{serial}{ext}")
        # distFilePath = os.path.join(Params.ROOT_DIR, r"Batches/%s_%s%s"%(fileNa,serial,ext))
        print("yzprt distFilePath:", distFilePath)
        with open(distFilePath, 'w', encoding='utf-8', errors='ignore') as f:
            f.write(ss.replace(f"deviceSerial = ''",f"deviceSerial = '{serial}'"))
if __name__ == '__main__':
    pass
    # u2s = U2Syner()
    # u2s.deviceList2Dict()
    # for i in range(len(u2s.deviceList)):
    #     d = u2s.deviceList[i]
    #     print("yzprt d.serial:", d.serial, d)
    #     # with open(r"",'w',encoding='utf-8',errors='ignore') as f:
    #     #     f.write()
    # for k,v in u2s.deviceDict.items():
    #     print("yzprt k,v:", k,v)
    u2s = U2Syner()
    print("yzprt u2s.deviceList:", u2s.deviceList)
    refreshBatchCode(r"Service/U2DevTool.py", deviceList=u2s.deviceList)
    # f"deviceSerial = '{serial}'"
